<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-minimal.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"sekla.cn","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"right","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="123456789101112#include &quot;stdio.h&quot;#define H ?  &#x2F;&#x2F;定义常数H#define J ?  &#x2F;&#x2F;定义常数Jint array1[H][J]; int array2[J][H]; void f(int x, int y) &amp;#123;	array1[x][y]&#x3D;x+2*y;	array2[y][x]&#x3D;y-x*x;&amp;#125;int main">
<meta property="og:type" content="article">
<meta property="og:title" content="3个汇编求源码题">
<meta property="og:url" content="http://sekla.cn/2021/05/01/csapp-practice-three/index.html">
<meta property="og:site_name" content="Sekla&#39;s Blog">
<meta property="og:description" content="123456789101112#include &quot;stdio.h&quot;#define H ?  &#x2F;&#x2F;定义常数H#define J ?  &#x2F;&#x2F;定义常数Jint array1[H][J]; int array2[J][H]; void f(int x, int y) &amp;#123;	array1[x][y]&#x3D;x+2*y;	array2[y][x]&#x3D;y-x*x;&amp;#125;int main">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-05-01T10:00:00.000Z">
<meta property="article:modified_time" content="2021-10-18T14:36:36.695Z">
<meta property="article:author" content="Sekla">
<meta property="article:tag" content="汇编">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://sekla.cn/2021/05/01/csapp-practice-three/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>3个汇编求源码题 | Sekla's Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

  <script type="text/javascript" src="/js/my_js/clicklove.js"></script>
<!-- hexo injector head_end start -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-math@4.0.0/dist/style.css">
<!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Sekla's Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Keep Learning Keep Doing</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">14</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">23</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">100</span></a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://sekla.cn/2021/05/01/csapp-practice-three/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Sekla">
      <meta itemprop="description" content="Sekla">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Sekla's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          3个汇编求源码题
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-05-01 18:00:00" itemprop="dateCreated datePublished" datetime="2021-05-01T18:00:00+08:00">2021-05-01</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-10-18 22:36:36" itemprop="dateModified" datetime="2021-10-18T22:36:36+08:00">2021-10-18</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%B3%BB%E7%BB%9F/" itemprop="url" rel="index"><span itemprop="name">计算机系统</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>5.1k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>5 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;stdio.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> H ?  <span class="comment">//定义常数H</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> J ?  <span class="comment">//定义常数J</span></span></span><br><span class="line"><span class="keyword">int</span> array1[H][J]; </span><br><span class="line"><span class="keyword">int</span> array2[J][H]; </span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">f</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span> </span>&#123;</span><br><span class="line">	array1[x][y]=x+<span class="number">2</span>*y;</span><br><span class="line">	array2[y][x]=y-x*x;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">( )</span></span>&#123;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">f:</span><br><span class="line">pushl %ebp</span><br><span class="line">movl %esp, %ebp</span><br><span class="line">pushl %ebx</span><br><span class="line"></span><br><span class="line">movl 8(%ebp), %ecx</span><br><span class="line">movl 12(%ebp), %edx</span><br><span class="line">movl 12(%ebp), %eax</span><br><span class="line">addl %eax, %eax</span><br><span class="line">addl 8(%ebp), %eax</span><br><span class="line">sall $4, %ecx</span><br><span class="line">leal (%ecx,%edx), %edx</span><br><span class="line">movl %eax, array1(,%edx,4)</span><br><span class="line"></span><br><span class="line">movl 12(%ebp), %edx</span><br><span class="line">movl 8(%ebp), %ebx</span><br><span class="line">movl 8(%ebp), %eax</span><br><span class="line">imul 8(%ebp), %eax</span><br><span class="line">movl 12(%ebp), %ecx</span><br><span class="line">subl %eax, %ecx</span><br><span class="line">movl %edx, %eax</span><br><span class="line">sall $3, %eax</span><br><span class="line">addl %edx, %eax</span><br><span class="line">sall $2, %eax</span><br><span class="line">addl %edx, %eax</span><br><span class="line">addl %ebx, %eax</span><br><span class="line">movl %ecx, array2(,%eax,4)</span><br><span class="line"></span><br><span class="line">popl %ebx</span><br><span class="line">popl %ebp</span><br><span class="line">ret</span><br></pre></td></tr></table></figure>

<a id="more"></a>

<p>汇编代码意义，逐行：</p>
<p>1-4：ebp入栈，初始化栈顶指针，ebx入栈。</p>
<p>5-12：这一段为array1[x][y]=x+2*y;</p>
<p>ecx=8+ebp，edx=12+ebp，eax=12+ebp地址内容。将eax寄存器中数据累加，再将8+ebp处数据与eax寄存器中数据相加并存入eax，ecx寄存器数据算术左移4位，加载edx+ecx地址并赋予edx，将eax中的计算结果给予4*edx+array1中。</p>
<p>可知12+ebp为y，8+ebp为x，固需要计算4<em>edx的值，edx=ecx+edx，最后得到即(x&lt;&lt;4+y)\</em>4=(x+1)*(y+1);</p>
<p>13-25：这一段为array2[y][x]=y-x*x;</p>
<p>分别将edx=12+ebp，ebx=8+ebp，eax=8+ebp，将8+ebp的内容与eax中数据相乘，可知8+ebp即为x，12+ebp处为y，ecx=12+ebp，即y，ecx=ecx-eax，eax中算术左移3位，并将eax=eax+edx，eax算术左移2位，eax=eax+edx，eax=ebx+eax，将计算结果给4*eax+array2，((y&lt;&lt;3+y)&lt;&lt;2+y+x)*4=(x+1)*(y+1);</p>
<p>26-29:</p>
<p>弹出栈，结束函数。</p>
<p>根据以上两段汇编分析，由于数组为int型，根据公式可知x&lt;&lt;4+y=J<em>x+y；J=2^4=16；又知(y&lt;&lt;3+y)&lt;&lt;2+y+x=H\</em>y+x；H=(2^3+1)*2^2+1=37。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">aprod</span><span class="params">(<span class="keyword">int</span> a[], <span class="keyword">int</span> n)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">int</span> i, x, y, z;</span><br><span class="line">	<span class="keyword">int</span> r = <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">for</span> (i = <span class="number">0</span>; （  <span class="number">1</span>  ）; （  <span class="number">2</span>  ）) &#123;</span><br><span class="line">		（  <span class="number">3</span>  ）;</span><br><span class="line">		（  <span class="number">4</span>  ）;</span><br><span class="line">		（  <span class="number">5</span>  ）;</span><br><span class="line">		（  <span class="number">6</span>  ）;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span> (; i &lt; n; i++)</span><br><span class="line">       （  <span class="number">7</span>  ）;</span><br><span class="line">    <span class="keyword">return</span> r;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line">aprod:</span><br><span class="line">pushl%ebp</span><br><span class="line">movl %esp, %ebp </span><br><span class="line">subl $32, %esp</span><br><span class="line">movl $1, -20(%ebp)</span><br><span class="line">movl $0, -4(%ebp)</span><br><span class="line">jmp.L2</span><br><span class="line">.L3:</span><br><span class="line">movl -4(%ebp), %eax &#x2F;&#x2F;eax&#x3D;i</span><br><span class="line">sall $2, %eax &#x2F;&#x2F;i&#x3D;i*4;</span><br><span class="line">addl 8(%ebp), %eax &#x2F;&#x2F;i&#x3D;M[ebp+8]+i  即A[i]</span><br><span class="line">movl (%eax), %eax &#x2F;&#x2F;eax&#x3D;A[i]</span><br><span class="line">movl %eax, -8(%ebp) &#x2F;&#x2F;M[ebp-8]&#x3D;A[i] 可能是x</span><br><span class="line">movl -4(%ebp), %eax &#x2F;&#x2F;eax&#x3D;i</span><br><span class="line">addl $1, %eax &#x2F;&#x2F;i&#x3D;i+1</span><br><span class="line">sall $2, %eax &#x2F;&#x2F;i&#x3D;i*4</span><br><span class="line">addl 8(%ebp), %eax &#x2F;&#x2F;i&#x3D;M[ebp+8]+i 即A[i+1]</span><br><span class="line">movl (%eax), %eax &#x2F;&#x2F;eax&#x3D;A[i+1] </span><br><span class="line">movl %eax, -12(%ebp) &#x2F;&#x2F;M[ebp-12]&#x3D;A[i+1]; 可能是y</span><br><span class="line">movl -4(%ebp), %eax &#x2F;&#x2F;i&#x3D;M[ebp-4]</span><br><span class="line">addl $2, %eax &#x2F;&#x2F;</span><br><span class="line">sall $2, %eax</span><br><span class="line">addl 8(%ebp), %eax</span><br><span class="line">movl (%eax), %eax</span><br><span class="line">movl %eax, -16(%ebp)</span><br><span class="line">movl -20(%ebp), %eax</span><br><span class="line">imull -8(%ebp), %eax</span><br><span class="line">imull -12(%ebp), %eax</span><br><span class="line">imull -16(%ebp), %eax</span><br><span class="line">movl %eax, -20(%ebp)</span><br><span class="line">addl $3, -4(%ebp)</span><br><span class="line">.L2:</span><br><span class="line">movl 12(%ebp), %eax</span><br><span class="line">subl $2, %eax</span><br><span class="line">cmpl -4(%ebp), %eax</span><br><span class="line">jg.L3</span><br><span class="line">jmp.L4</span><br><span class="line">.L5:</span><br><span class="line">movl -4(%ebp), %eax</span><br><span class="line">sall $2, %eax</span><br><span class="line">addl 8(%ebp), %eax</span><br><span class="line">movl (%eax), %eax</span><br><span class="line">movl -20(%ebp), %edx</span><br><span class="line">imull %edx, %eax</span><br><span class="line">movl %eax, -20(%ebp)</span><br><span class="line">addl $1, -4(%ebp)</span><br><span class="line">.L4:</span><br><span class="line">movl -4(%ebp), %eax</span><br><span class="line">cmpl 12(%ebp), %eax</span><br><span class="line">jl.L5</span><br><span class="line">movl -20(%ebp), %eax</span><br><span class="line">leave</span><br><span class="line">ret</span><br></pre></td></tr></table></figure>

<p>1-7:初始化栈指针，esp=esp-32,分配空间，将ebp-20位置赋值1，即r在ebp-20，ebp-4为i，跳转到L2位置。</p>
<p>32-36：L2段汇编代码，首先将M[ebp+12]位置数据给eax后，eax=eax-2，比较ebp-4和eax，若M(ebp-4)&gt;eax,则跳转到L3，开始for循环。</p>
<p>8-31：L3段汇编代码，eax=i，eax=eax&lt;&lt;2，eax=M[8+ebp]+eax， 将eax中地址数据给eax，ebp-8=eax，eax=ebp-4，eax=eax+1，eax=eax&lt;&lt;2，eax=M[8+ebp]+eax，eax=M[eax]，M[ebp-12]=eax，eax=eax+2，eax=eax&lt;&lt;2，eax=M[ebp+8]，eax=M[eax]，M[ebp-16]=eax，eax=M[ebp-20]，eax=M[ebp-8]*eax，eax=M[ebp-12]*eax，eax=M[ebp-16]*eax，M[ebp-20]=eax，i=i+3;</p>
<p>根据数组位移判断，每次前几段为A[i]，以下几段类比即可。</p>
<p>47-49：L4段，根据源代码和汇编代码可知，M[12+ebp]=n,比较若小于，则跳转L5.</p>
<p>38-46：L5段，将M[ebp-20]给eax寄存器，</p>
<p>51-53：最后根据源代码，返回r的值，则M[ebp-20]=r。</p>
<p>代码补全</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">aprod</span><span class="params">(<span class="keyword">int</span> a[], <span class="keyword">int</span> n)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">int</span> i, x, y, z;</span><br><span class="line">	<span class="keyword">int</span> r = <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">for</span> (i = <span class="number">0</span>;i&gt;n<span class="number">-2</span>;i=i+<span class="number">3</span>) &#123;</span><br><span class="line">		x=a[i];</span><br><span class="line">		y=a[i+<span class="number">1</span>];</span><br><span class="line">		z=a[i+<span class="number">2</span>];</span><br><span class="line">		r=x*y*z;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span> (; i &lt; n; i++)</span><br><span class="line">      	r=r*a[i];</span><br><span class="line">    <span class="keyword">return</span> r;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">frac</span><span class="params">(<span class="keyword">int</span> a)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">if</span>(a&lt;<span class="number">2</span>) <span class="keyword">return</span> _______;</span><br><span class="line">	<span class="keyword">return</span> ________;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">sum</span><span class="params">(<span class="keyword">int</span> a,<span class="keyword">int</span> b)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> c=_______;</span><br><span class="line">	<span class="keyword">return</span> ______;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> i=<span class="number">10</span>,j=<span class="number">6</span>;</span><br><span class="line">	<span class="keyword">int</span> k=sum(_____,______);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>逐行解释：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line">对应的汇编代码如下：</span><br><span class="line">frac:</span><br><span class="line">pushl %ebp</span><br><span class="line">movl %esp, %ebp</span><br><span class="line">subl $24, %esp</span><br><span class="line">cmpl $1, 8(%ebp) &#x2F;&#x2F;栈初始化 esp&#x3D;esp-24,比较1和a,如果a&gt;2，跳转L2</span><br><span class="line">jg.L2</span><br><span class="line">movl 8(%ebp), %eax &#x2F;&#x2F;eax&#x3D;a</span><br><span class="line">imull 8(%ebp), %eax &#x2F;&#x2F;eax&#x3D;a*a </span><br><span class="line">jmp.L3 &#x2F;&#x2F;跳转L3</span><br><span class="line">.L2:</span><br><span class="line">movl 8(%ebp), %eax &#x2F;&#x2F;eax&#x3D;a</span><br><span class="line">subl $2, %eax &#x2F;&#x2F;a&#x3D;a-2</span><br><span class="line">movl %eax, (%esp) &#x2F;&#x2F;M[esp]&#x3D;a</span><br><span class="line">call frac &#x2F;&#x2F;调用frac(a-2)</span><br><span class="line">addl 8(%ebp), %eax &#x2F;&#x2F;eax&#x3D;frac(a-2)+a</span><br><span class="line">.L3:</span><br><span class="line">leave</span><br><span class="line">ret</span><br><span class="line">sum:</span><br><span class="line">pushl %ebp &#x2F;&#x2F;ebp入栈</span><br><span class="line">movl %esp, %ebp </span><br><span class="line">subl $40, %esp &#x2F;&#x2F;初始化栈</span><br><span class="line">movl 12(%ebp), %eax &#x2F;&#x2F;eax&#x3D;M[ebp+12]</span><br><span class="line">movl 8(%ebp), %edx &#x2F;&#x2F;edx&#x3D;M[ebp+8]</span><br><span class="line">addl %edx, %eax &#x2F;&#x2F;eax&#x3D;edx+eax</span><br><span class="line">movl %eax, (%esp) &#x2F;&#x2F;M[esp]&#x3D;eax</span><br><span class="line">call frac &#x2F;&#x2F;调用frac(a+b)</span><br><span class="line">movl %eax, -12(%ebp) </span><br><span class="line">movl -12(%ebp), %eax</span><br><span class="line">addl %eax, %eax &#x2F;&#x2F;2*c</span><br><span class="line">addl 12(%ebp), %eax &#x2F;&#x2F; 返回2*c+b</span><br><span class="line">leave</span><br><span class="line">ret</span><br><span class="line">main: </span><br><span class="line">pushl %ebp</span><br><span class="line">movl %esp, %ebp</span><br><span class="line">andl $-16, %esp</span><br><span class="line">subl $32, %esp &#x2F;&#x2F;初始化栈 </span><br><span class="line">movl $10, 20(%esp) &#x2F;&#x2F;i&#x3D;10</span><br><span class="line">movl $6, 24(%esp) &#x2F;&#x2F;j&#x3D;6</span><br><span class="line">movl 20(%esp), %eax &#x2F;&#x2F;eax&#x3D;i</span><br><span class="line">movl 24(%esp), %edx &#x2F;&#x2F;edx&#x3D;j</span><br><span class="line">subl %eax, %edx &#x2F;&#x2F;j&#x3D;j-i</span><br><span class="line">movl 20(%esp), %eax </span><br><span class="line">addl $1, 20(%esp) &#x2F;&#x2F;i++</span><br><span class="line">movl %edx, 4(%esp)</span><br><span class="line">movl %eax, (%esp)</span><br><span class="line">call sum &#x2F;&#x2F;调用sum&#x3D;(i++,j-1)</span><br><span class="line">movl %eax, 28(%esp) &#x2F;&#x2F;k&#x3D;sum</span><br><span class="line">movl $0, %eax &#x2F;&#x2F;return 0</span><br><span class="line">leave</span><br><span class="line">ret</span><br></pre></td></tr></table></figure>

<p>补全代码：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">frac</span><span class="params">(<span class="keyword">int</span> a)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"><span class="keyword">if</span>(a&lt;<span class="number">2</span>) <span class="keyword">return</span> a*a;</span><br><span class="line"><span class="keyword">return</span> a+frac(a<span class="number">-2</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">sum</span><span class="params">(<span class="keyword">int</span> a,<span class="keyword">int</span> b)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"><span class="keyword">int</span> c=frac(a+b);</span><br><span class="line"><span class="keyword">return</span> <span class="number">2</span>*c+b;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(</span></span></span><br><span class="line"><span class="function"><span class="params">&#123;</span></span></span><br><span class="line"><span class="function"><span class="params"><span class="keyword">int</span> i=<span class="number">10</span>,j=<span class="number">6</span>;</span></span></span><br><span class="line"><span class="function"><span class="params"><span class="keyword">int</span> k=sum(i++,j-i);</span></span></span><br><span class="line"><span class="function"><span class="params"><span class="keyword">return</span> <span class="number">0</span>;</span></span></span><br><span class="line"><span class="function"><span class="params">&#125;</span></span></span><br></pre></td></tr></table></figure>


    </div>
    
    
    

      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/tags/%E6%B1%87%E7%BC%96/" rel="tag"><i class="fa fa-tag"></i> 汇编</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/04/19/C++%20STL/" rel="prev" title="深入理解C++ STL(1)基础与迭代器">
      <i class="fa fa-chevron-left"></i> 深入理解C++ STL(1)基础与迭代器
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/05/14/csapp-bomblab/" rel="next" title="CSAPP-Bomblab">
      CSAPP-Bomblab <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Sekla"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Sekla</p>
  <div class="site-description" itemprop="description">Sekla</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">100</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">23</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/ShenTiao" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;ShenTiao" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:584892986@qq.com" title="E-Mail → mailto:584892986@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

      <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=1416875904&auto=0&height=66"></iframe>
      
    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Sekla</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">288k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">4:22</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script size="300" alpha="0.6" zIndex="-1" src="/lib/canvas-ribbon/canvas-ribbon.js"></script>
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  
      

<script>
  if (typeof MathJax === 'undefined') {
    window.MathJax = {
      loader: {
        source: {
          '[tex]/amsCd': '[tex]/amscd',
          '[tex]/AMScd': '[tex]/amscd'
        }
      },
      tex: {
        inlineMath: {'[+]': [['$', '$']]},
        tags: 'ams'
      },
      options: {
        renderActions: {
          findScript: [10, doc => {
            document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
              const display = !!node.type.match(/; *mode=display/);
              const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
              const text = document.createTextNode('');
              node.parentNode.replaceChild(text, node);
              math.start = {node: text, delim: '', n: 0};
              math.end = {node: text, delim: '', n: 0};
              doc.math.push(math);
            });
          }, '', false],
          insertedScript: [200, () => {
            document.querySelectorAll('mjx-container').forEach(node => {
              let target = node.parentNode;
              if (target.nodeName.toLowerCase() === 'li') {
                target.parentNode.classList.add('has-jax');
              }
            });
          }, '', false]
        }
      }
    };
    (function () {
      var script = document.createElement('script');
      script.src = '//cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js';
      script.defer = true;
      document.head.appendChild(script);
    })();
  } else {
    MathJax.startup.document.state(0);
    MathJax.texReset();
    MathJax.typeset();
  }
</script>

    

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/shizuku.model.json"},"display":{"position":"left","width":250,"height":500},"mobile":{"show":false},"react":{"opacity":0.9},"log":false});</script></body>
</html>

<script type="text/javascript" src="/js/src/love.js"></script>
